#ifndef _BARTER_SERVER_CONNECTION_H_
#define _BARTER_SERVER_CONNECTION_H_

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/utility.hpp>

#include "NetworkMessage.h"
#include "LoginModule.h"
#include "CashierBackend.h"

class NetworkSystem;

enum ConnectionState
{
	STATE_CLOSING,
	STATE_RUNNING,
};

class Connection
{
	friend class NetworkSystem;
private:
	LoginModule Login;
	CashierBackend Cashier;
	int LoginID, CashierID;
	boost::asio::ip::tcp::socket Socket;
	Connection(boost::asio::io_service& h) : Socket(h), State(STATE_RUNNING) {}
	NetworkMessage ReadBuffer; // current read operation
	NetworkMessage WriteBuffer; // current write operation
	int PendingWrite, PendingRead;
	std::list<NetworkMessage*> PendingOutput;
	int State;
	boost::recursive_mutex ReadMutex, WriteMutex;
	void OnAccept();
	void InternalSend();
	void ParseHeader();
	Connection();
public:
	~Connection();
	void OnRead(const boost::system::error_code&);
	void OnWrite(const boost::system::error_code&);
	void Send(NetworkMessage*);
};

#endif // _BARTER_SERVER_CONNECTION_H_
